package leetcode.part3;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class S290 {
    public boolean wordPattern(String pattern, String s) {
        String[] s1 = pattern.split("");
        String[] s2 = s.split(" ");
        if (s1.length != s2.length) return false;
        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < s1.length; i++) {
            if (map.containsKey(s1[i])) {
                if (!Objects.equals(map.get(s1[i]), s2[i])) {
                    return false;
                }
                // 这里比较过了就会直接跳到下个循环，不会执行接下来的逻辑了,if-else

            } else {
                // 判断当前字符对应的t中的字符已经和s前面的某个字符建立过映射关系
                if (map.containsValue(s2[i])) return false;
                map.put(s1[i], s2[i]);
            }

        }
        return true;

    }

    @Test
    public void test() {
        String pattern = "abba", s = "dog cat cat dog";
        String pattern1 = "abba", s1 = "dog cat cat fish";
        String pattern2 = "aaaa", s2 = "dog cat cat dog";
        System.out.println(wordPattern(pattern, s));
        System.out.println(wordPattern(pattern1, s1));
        System.out.println(wordPattern(pattern2, s2));
    }
}
